package com.youhome.service.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.mapper.CounselorRecommendHouseMapper;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.BUser;
import com.youhome.service.model.CounselorRecommendHouse;
import com.youhome.service.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2018/09/17.
 */
@Service
public class CounselorRecommendHouseServiceImpl implements CounselorRecommendHouseService {
    @Autowired
    private CounselorRecommendHouseMapper counselorRecommendHouseMapper;
    @Autowired
    private BUserService bUserService;
    @Autowired
    private BCounselorService counselorService;
    @Autowired
    private HouseService houseService;
    @Autowired
    private BClientService clientService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return counselorRecommendHouseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByBean(CounselorRecommendHouse record) {
        return counselorRecommendHouseMapper.deleteByBean(record);
    }

    @Override
    public int insert(CounselorRecommendHouse record) {
        record.setCreateTime(new Date());
        return counselorRecommendHouseMapper.insert(record);
    }

    @Override
    public int insertSelective(CounselorRecommendHouse record) {
        record.setCreateTime(new Date());
        return counselorRecommendHouseMapper.insertSelective(record);
    }

    @Override
    public CounselorRecommendHouse selectByPrimaryKey(Long id) {
        return counselorRecommendHouseMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<CounselorRecommendHouse> selectByBean(Integer pageNumber, Integer pageSize, CounselorRecommendHouse record) {
        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<CounselorRecommendHouse> counselorRecommendHouses = counselorRecommendHouseMapper.selectByBean(record);

        return new PageInfo<>(counselorRecommendHouses);
    }

    @Override
    public int updateByPrimaryKeySelective(CounselorRecommendHouse record) {
        record.setUpdateTime(new Date());
        return counselorRecommendHouseMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CounselorRecommendHouse record) {
        record.setUpdateTime(new Date());
        return counselorRecommendHouseMapper.updateByPrimaryKey(record);
    }

    @Override
    public boolean selectExistsByBean(CounselorRecommendHouse record) {
        if (counselorRecommendHouseMapper.selectExistsByBean(record) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public void recommendHouse(String phone, Long clientId, String houseId, String remark) throws CustomException {
        if (StringUtils.isEmpty(clientId) || StringUtils.isEmpty(houseId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }

        BUser bUser = bUserService.selectByPhone(phone);
        BCounselor bCounselor = null;
        if (bUser != null) {
            bCounselor = counselorService.selectByUserId(bUser.getUserId());
        }
        if (bCounselor == null) {
            throw new CustomException(ResultCode.LAND_EXPIRATION);
        }
        if (!houseService.selectExistsByPrimaryKey(houseId)) {
            throw new CustomException(ResultCode.HOUSE_INEXISTENCE);
        }
        if (!clientService.selectExistsByPrimaryKey(clientId)) {
            throw new CustomException(ResultCode.CLIENT_NOT_EXIST);
        }
        CounselorRecommendHouse record = new CounselorRecommendHouse();
        record.setCounselorId(bCounselor.getCounselorId());
        record.setClientId(clientId);
        record.setHouseId(houseId);
        record.setIsDelete(false);

        if (!StringUtils.isEmpty(remark)) {
            record.setRemark(remark);
        }
        if (counselorRecommendHouseMapper.selectExistsByBean(record) > 0) {
            throw new CustomException(ResultCode.HAVE_RECOMMENDED);
        }
        record.setCreateTime(new Date());
        if (counselorRecommendHouseMapper.insert(record) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public void deleteRecommendHouse(String phone, Long clientId, String houseId) throws CustomException {

        BUser bUser = bUserService.selectByPhone(phone);
        BCounselor bCounselor = null;
        if (bUser != null) {
            bCounselor = counselorService.selectByUserId(bUser.getUserId());
        }

        if (bCounselor == null) {
            throw new CustomException(ResultCode.LAND_EXPIRATION);
        }

        CounselorRecommendHouse record = new CounselorRecommendHouse();

        record.setHouseId(houseId);
        record.setCounselorId(bCounselor.getCounselorId());
        record.setIsDelete(true);

        if (counselorRecommendHouseMapper.selectExistsByBean(record) > 0) {
            throw new CustomException(ResultCode.ALREADY_DELETED);
        }

        if (counselorRecommendHouseMapper.deleteByBean(record) == 0) {
            throw new CustomException(ResultCode.FAIL);
        }
    }

    @Override
    public PageInfo<CounselorRecommendHouse> selectByBean(String phone, Long clientId, Integer pageNumber, Integer pageSize) throws CustomException {

        if (StringUtils.isEmpty(clientId)) {
            throw new CustomException(ResultCode.ERROR_PARAMETER);
        }
        CounselorRecommendHouse record = new CounselorRecommendHouse();
        BUser bUser = bUserService.selectByPhone(phone);
        BCounselor bCounselor = null;
        if (bUser != null) {
            bCounselor = counselorService.selectByUserId(bUser.getUserId());
        }
        if (bCounselor != null) {
            record.setCounselorId(bCounselor.getCounselorId());
        }
        record.setIsDelete(false);

        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(counselorRecommendHouseMapper.selectByBean(record));
    }
}
